package com.jshoperxms.controller.api.langdu;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.SysMsgRpT;
import com.jshoperxms.service.MemberTService;
import com.jshoperxms.service.SysMsgRpTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 2018/11/26.
 * Des:
 */
@RestController
@RequestMapping("/api/minecenter/msg")
public class MineMsgController extends BaseTController{

    @Resource
    private SysMsgRpTService sysMsgRpTService;


    /**
     * 获取我的消息记录
     * @param rq
     * @return
     */
    @RequestMapping(value="/findMineMsglist",method= RequestMethod.POST)
    public ResponseEntity findMineMsglist(@RequestBody SearchModel rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getMemberid())){
            Criterion criterion= Restrictions.and(Restrictions.eq("receiverid",rq.getMemberid())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
            int currentPage=rq.getStart();
            int lineSize=rq.getLength();
            int records=this.sysMsgRpTService.count(SysMsgRpT.class,criterion).intValue();
            List<SysMsgRpT> list=this.sysMsgRpTService.findByCriteriaByPage(SysMsgRpT.class,criterion, Order.desc(TagWords.CREATETIME),currentPage,lineSize);
            rq.setRecordsTotal(records);
            responsedata.put("results",list);
            responsedata.put("start",rq.getNextPageNo());
            responsedata.put("totalPage",rq.getTotalPage());
            responsedata.put("totalrecords",records);
            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }



    @RequestMapping(value="/delAllMyMsg",method= RequestMethod.POST)
    public ResponseEntity delAllMyMsg(@RequestBody SearchModel rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getMemberid())){
            Criterion criterion=Restrictions.eq("receiverid",rq.getMemberid());
            List<SysMsgRpT>list=this.sysMsgRpTService.findByCriteria(SysMsgRpT.class,criterion);
            if(list.size()>0){
                this.sysMsgRpTService.deleteAll(list);
            }

            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }
}
